<?php

namespace app\admin\controller\addfamily;

use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Response;

/**
 * 创建家庭
 *
 */
class Chuangjian extends Backend
{
    protected $model = null;
    protected $noNeedRight = ['table1', 'table2'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\family\Home;
    }

    public function index()
    {

        $admin = $this->auth->getUserInfo();
        // var_dump($admin);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params['number'] = Random::build();
            $params['home_ad_id'] = $admin['id'];
            $params['home_admin'] = $admin['username'];
            $params['add_time'] = time();
            // var_dump($params);

            $add = $this->model->insert($params);
            if ($add) {
                $access = array(
                    "group_id" => 2,
                );
                if ($admin['id'] > 1) {
                    $home = $this->model->where("number", $params['number'])->select();
                    $home_id = array("home_id" =>  $home[0]["id"]);
                    Db::table('jc_auth_group_access')->where("uid", $admin['id'])->update($access);
                    Db::table('jc_admin')->where("id", $admin['id'])->update($home_id);
                }
                $this->auth->logout();
                // $this->success('添加成功', 'index/logout', '', 1, []);
                
            } else {
                $this->error('添加失败');
            };
        }

        return $this->view->fetch();
    }
    function logout()
    {
        // 创建 Indexz 控制器实例
        $index = new \app\admin\controller\Index();

        // 调用 Indexz 控制器中的 logout 方法
        $index->logout();
    }
}
